﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace ControllerConsole
{
    class Program
    {
        static SerialPort _port = new SerialPort("COM8");
        static void InitComm()
        {
            _port.BaudRate = 9600;
            _port.Parity = Parity.None;
            _port.StopBits = StopBits.One;
            _port.DataBits = 8;
            _port.Handshake = Handshake.None;
            _port.DataReceived += _port_DataReceived;
            _port.Open();
        }

        static void _port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (e.EventType == System.IO.Ports.SerialData.Chars)
            {
                Console.Write(_port.ReadExisting());
            }
        }
        static void Main(string[] args)
        {
            InitComm();
            Console.WriteLine("Started");
           loop:
            byte[] b = ReadString();
            _port.Write(b, 0, 1);

            if (b[0] != 'q')
            {
                goto loop;
            }
        }

        static byte[] ReadString()
        {
            string read = Console.ReadLine();
            return ASCIIEncoding.ASCII.GetBytes(read);
        }
    }
}
